/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.data.core;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.user.client.ui.Widget;

/**
 * A concrete implementation of {@link DataManagerCollection} that delegates
 * work to {@code DataManagerCollection} that are added to it. This is a great
 * class to use if you have multiple {@code DataManagerCollection}s and want
 * all of them to be used.
 * 
 * @author Arthur Kalmenson
 */
public class DataManagerCollectionImpl implements DataManagerCollection {

	/**
	 * The set of {@link DataManagerCollection} to go over.
	 */
	private Set<DataManagerCollection> dataManagerCollectionSet;

	/**
	 * Creates a new {@code DataManagerCollectionImpl}.
	 */
	public DataManagerCollectionImpl() {
		dataManagerCollectionSet = new HashSet<DataManagerCollection>();
	}

	public DataManager<?, ?> getDataManager(Widget widget) {

		DataManager<?, ?> result = null;
		
		for (DataManagerCollection dataManagerCollection : dataManagerCollectionSet) {
			if (dataManagerCollection.hasDataManager(widget)) {
				result = dataManagerCollection.getDataManager(widget);
				break;
			}
		}

		return result;
	}

	public boolean hasDataManager(Widget widget) {
		
		boolean result = false;
		
		for (DataManagerCollection dataManagerCollection : dataManagerCollectionSet) {
			if (dataManagerCollection.hasDataManager(widget)) {
				result = true;
				break;
			}
		}
		return result;
	}

	/**
	 * Adds a {@link DataManagerCollection} for this
	 * {@code DataManagerCollectionImpl} to use.
	 * 
	 * @param dataManagerCollection
	 *            the {@link DataManagerCollection} to add.
	 */
	public void addDataManagerCollection(
			DataManagerCollection dataManagerCollection) {
		dataManagerCollectionSet.add(dataManagerCollection);
	}
}
